package bookoperations;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 34255
 * Date: 2024-05-25
 * Time: 16:02
 */
public class AddOperation implements IBookOperations{
    @Override
    public void work(BookList booklist) {
        if(booklist.getUseSize() == booklist.getBooksLength()){//判断书架是否已满？
            System.out.println("书架已满,无法再添加图书!!!");
            return;
        }
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要添加的图书名称");
        String name = scanner.nextLine();
        System.out.println("请输入要添加的图书作者");
        String author = scanner.nextLine();
        System.out.println("请输入要添加的图书价格");
        int price = scanner.nextInt();
        scanner.nextLine();
        System.out.println("请输入要添加的图书类型");
        String type = scanner.nextLine();

        for (int i = 0; i < booklist.getUseSize(); i++) {
            if(booklist.getBook(i).getName().equals(name)){
                System.out.println("该图书已存在！！！");
                return;
            }
        }

        Book book = new Book(name,author,price,type);//创建对象
        int pos = booklist.getUseSize();
        booklist.setBook(pos,book);

        booklist.setUseSize(booklist.getUseSize() + 1);
        System.out.println("添加图书成功！！！");
    }
}
